home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / lyrics / LeoslyricsParser.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  2.8 KB  |  89 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright (C) 2006 Jonathan Matthew
  4. # Copyright (C) 2007 James Livingston
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  12. # GStreamer plugins to be used and distributed together with GStreamer
  13. # and Rhythmbox. This permission is above and beyond the permissions granted
  14. # by the GPL license by which Rhythmbox is covered. If you modify this code
  15. # you may extend this exception to your version of the code, but you are not
  16. # obligated to do so. If you do not wish to do so, delete this exception
  17. # statement from your version.
  18. #
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program; if not, write to the Free Software
  27. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  28.  
  29.  
  30. import urllib
  31. import re
  32. import rb
  33.  
  34. # Python 2.4 compatibility
  35. try:
  36.     from xml.etree import cElementTree
  37. except:
  38.     import cElementTree
  39.  
  40.  
  41.  
  42. class LeoslyricsParser(object):
  43.     def __init__(self, artist, title):
  44.         self.artist = artist
  45.         self.title = title
  46.     
  47.     def search(self, callback, *data):
  48.         artist = urllib.quote(self.artist)
  49.         title = urllib.quote(self.title)
  50.  
  51.         htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
  52.             
  53.         loader = rb.Loader()
  54.         loader.get_url (htstring, self.got_lyrics, callback, *data)
  55.  
  56.     def got_lyrics (self, lyrics, callback, *data):
  57.         if lyrics is None:
  58.             callback (None, *data)
  59.             return
  60.  
  61.         element = cElementTree.fromstring(lyrics)
  62.         if element.find("response").attrib['code'] is not '0':
  63.             print "got failed response:" + lyrics
  64.             callback (None, *data)
  65.             return
  66.  
  67.         #FIXME: check non-exact matches
  68.         match = element.find("searchResults").find("result")
  69.         if match.attrib["exactMatch"] is None:
  70.             print "no exact match:" + lyrics
  71.             callback (None, *data)
  72.             return
  73.  
  74.         lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(match.attrib["hid"].encode('utf-8')))
  75.         loader = rb.Loader()
  76.         loader.get_url (lurl, self.parse_lyrics, callback, *data)
  77.             
  78.     def parse_lyrics(self, result, callback, *data):
  79.         if result is None:
  80.             callback (None, *data)
  81.             return
  82.  
  83.         element = cElementTree.fromstring(result)
  84.  
  85.         lyrics = element.find('lyric').find('text').text
  86.         lyrics += "\n\nLyrics provided by leoslyrics.com"
  87.  
  88.         callback (lyrics, *data)
  89.